%macro get_f1099b;


	proc printto new log = "&logdir./get_f1099b.txt";
	run;


	/****************************************************
	*			Put individuals into a tinlist			*
	****************************************************/
	%make_tinlist;


	

			options errors = 0;
				proc sql noprint;
				[connection details redacted]
					create table sample as
					select * from connection to iq 
					(
					select distinct
						a.[redacted] as person_id,
						case when a.[redacted] = 4 then 3
							else a.[redacted] end as code,
						sum(coalesce(a.[redacted],0)
							- coalesce(a.[redacted],0)
							- coalesce(a.[redacted],0)) as gain,
						sum(a.[redacted]) as wh
					from [redacted] as a
						inner join #tinlist as b
							on(a.[redacted] = b.person_id)
					where a.[redacted] = &base_yr
						and a.[redacted]<=4
						and a.[redacted] = "D"
					group by a.[redacted], 
						code
					order by a.[redacted],
						code
					);
				disconnect from iq;
				quit;


		%do i = 1 %to 3;
			data gain&i;
				set sample(where=(code=&i));
				gain&i = gain;
				keep person_id gain&i;
			run;
		%end;

		/* withholding */
		proc means data = sample nway noprint;
			class person_id;
			output out = wh(drop=_type_ _freq_)
				sum(wh) = f1099b_wh;
		run;

		data rlib.f1099b;
			merge gain1 gain2 gain3 wh;
			by person_id;
		run;



	proc printto;
	run;

	


%mend;
